package com.ct.controller.system;

import com.ct.application.system.model.po.AdminSystemDepartmentSavePO;
import com.ct.application.system.model.vo.AdminSystemDepartmentVO;
import com.ct.application.system.service.IAdminSystemDepartmentService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.ApiLog;
import com.ct.commons.annotation.HasAdmin;
import com.ct.commons.annotation.PermissionApi;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import com.ct.system.orm.AdminSystemDepartment;
import com.ct.system.responsity.admin.IAdminSystemDepartmentRepository;
import com.ct.utils.threadlocal.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台管理-部门管理
 * @author Auto Generator
 * @since 2023-08-16
 */
@RestController
@PermissionApi(title = "部门管理模块",path="/api/v1/department",code = "DP1000",sorted = 20)
public class AdminSystemDepartmentController extends BaseController {

    @Autowired
    private IAdminSystemDepartmentService adminSystemDepartmentService;
    @Autowired
    private IAdminSystemDepartmentRepository adminSystemDepartmentRepository;


    @PostMapping("/department/saveupdate")
    @ApiLog("部门保存和更新")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "部门保存和更新",path="/api/v1/department/saveupdate",code = "DP1001",sorted = 1)
    public Boolean saveUpdateAdminSystemDepartment(@Validated @RequestBody AdminSystemDepartmentSavePO adminSystemDepartmentSavePO) {
        return adminSystemDepartmentService.saveUpdateAdminSystemDepartment(adminSystemDepartmentSavePO);
    }

    /**
     * 根据id查询部门
     *
     * @param id
     * @return
     */
    @PostMapping("/department/get/{id}")
    @PermissionApi(title = "根据id查询部门",path="/api/v1/department/get/{id}",code = "DP1002",sorted = 2)
    public AdminSystemDepartmentVO getAdminSystemDepartmentById(@PathVariable("id") Long id) {
        return adminSystemDepartmentService.getAdminSystemDepartmentById(id);
    }

    /**
     * 根据id删除部门
     *
     * @param id
     * @return
     */
    @PostMapping("/department/del/{id}")
    @ApiLog("根据id删除部门")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "根据id删除部门",path="/api/v1/department/del/{id}",code = "DP1003",sorted = 3)
    public Boolean deleteAdminSystemDepartmentById(@PathVariable("id") Long id) {
        return adminSystemDepartmentService.deleteAdminSystemDepartmentById(id);
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/department/status")
    @ApiLog("部门修改状态")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "部门修改状态",path="/api/v1/department/status",code = "DP1004",sorted = 4)
    public Boolean updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return adminSystemDepartmentService.updateStatus(id, field, Integer.parseInt(value));
        }else{
            return adminSystemDepartmentService
                    .updateStatusStr(id, field, String.valueOf(value));
        }
    }


    /**
     * 查询部门-tree
     * @param
     * @return
     */
    @PostMapping("/department/tree")
    @ApiLog("查询部门-tree")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "查询部门-tree",path="/api/v1/department/tree",code = "DP1005",sorted = 5)
    public List<AdminSystemDepartment> saveUpdateAdminSystemDepartmentTree() {
        return adminSystemDepartmentRepository.findPageTree(UserThreadLocal.get().getSystemId());
    }

}
